<template>
  <div class="role-permission-system">
    <div class="header">
      <div class="header-content">
        <div class="header-left">
          <i class="fas fa-user-shield header-icon"></i>
          <div>
            <h1>角色菜单配置</h1>
            <p class="header-subtitle">管理系统角色和菜单权限分配</p>
          </div>
        </div>
        <div class="header-stats">
          <div class="stat-item">
            <span class="stat-number">{{ roles.length }}</span>
            <span class="stat-label">角色数量</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">{{ totalMenuCount }}</span>
            <span class="stat-label">菜单总数</span>
          </div>
        </div>
      </div>
    </div>
    
    <div class="content">
      <!-- 左侧角色面板 -->
      <div class="role-panel">
        <div class="panel-header">
          <h2><i class="fas fa-users"></i> 角色列表 ({{ roles.length }})</h2>
          <button @click="showRoleForm" class="btn btn-primary btn-sm">
            <i class="fas fa-plus"></i> 添加角色
          </button>
        </div>
        <div class="panel-content">
          <div class="role-search">
            <i class="fas fa-search search-icon"></i>
            <input 
              type="text" 
              v-model="roleSearch" 
              placeholder="搜索角色..." 
              class="search-input"
            >
          </div>
          <div class="role-list">
            <div 
              v-for="role in filteredRoles" 
              :key="role.id"
              class="role-item"
              :class="{ 
                active: selectedRole && selectedRole.id === role.id,
                'super-admin': role.name === 'admin'
              }"
              @click="selectRole(role)"
            >
              <div class="role-icon" :class="getRoleIconClass(role)">
                <i :class="getRoleIcon(role)"></i>
              </div>
              <div class="role-info">
                <div class="role-name">
                  {{ role.name }}
                  <i v-if="role.name === 'admin'" class="fas fa-crown super-admin-badge" title="超级管理员"></i>
                  <span v-if="role.name === 'admin'" class="role-tag super-admin-tag">超级管理员</span>
                  <span v-else class="role-tag">普通角色</span>
                </div>
                <div class="role-desc">{{ role.description }}</div>
                <div class="role-meta">
                  <span class="permission-count">
                    <i class="fas fa-key"></i>
                    {{ role.permissions ? role.permissions.length : 0 }} 个权限
                  </span>
                </div>
              </div>
              <!-- 使用内联样式确保按钮可见 -->
              <div class="role-actions" style="display: flex; opacity: 1; visibility: visible;">
                <button class="btn-icon" @click.stop="editRole(role)" title="编辑角色">
                  <i class="fas fa-edit"></i>
                </button>
                <button 
                  class="btn-icon" 
                  @click.stop="deleteRole(role)" 
                  title="删除角色"
                  :disabled="role.name === 'admin'"
                >
                  <i class="fas fa-trash"></i>
                </button>
              </div>
            </div>
          </div>
          
          <div v-if="filteredRoles.length === 0" class="empty-state">
            <i class="fas fa-users"></i>
            <p>暂无角色数据</p>
          </div>
        </div>
      </div>
      
      <!-- 右侧菜单权限面板 -->
      <div class="menu-panel">
        <div class="panel-header">
          <div class="panel-title">
            <h2>
              <i class="fas fa-list"></i> 菜单权限管理
              <span v-if="selectedRole" class="selected-role"> - {{ selectedRole.name }}</span>
            </h2>
            <div v-if="selectedRole" class="permission-stats">
              <span class="stat-badge">
                <i class="fas fa-check-circle"></i>
                已选 {{ getSelectedMenuCount() }} / {{ totalMenuCount }} 个菜单
              </span>
            </div>
          </div>
          <div class="panel-actions">
            <button 
              class="btn btn-secondary btn-sm" 
              @click="expandAllMenus"
              :disabled="!selectedRole"
            >
              <i class="fas fa-expand"></i> 展开全部
            </button>
            <button 
              class="btn btn-secondary btn-sm" 
              @click="collapseAllMenus"
              :disabled="!selectedRole"
            >
              <i class="fas fa-compress"></i> 收起全部
            </button>
            <button 
              class="btn btn-secondary btn-sm" 
              @click="selectAllMenus"
              :disabled="!selectedRole"
            >
              <i class="fas fa-check-square"></i> 全选
            </button>
            <button 
              class="btn btn-primary btn-sm" 
              @click="savePermissions"
              :disabled="!selectedRole || !hasPermissionChanges"
            >
              <i class="fas fa-save"></i> 保存权限
            </button>
          </div>
        </div>
        <div class="panel-content">
          <div v-if="selectedRole" class="menu-container">
            <div class="menu-header">
              <div class="menu-search">
                <i class="fas fa-search search-icon"></i>
                <input 
                  type="text" 
                  v-model="menuSearch" 
                  placeholder="搜索菜单..." 
                  class="search-input"
                >
              </div>
              <div class="menu-actions">
                <button 
                  class="btn btn-outline btn-sm" 
                  @click="clearAllPermissions"
                  :disabled="getSelectedMenuCount() === 0"
                >
                  <i class="fas fa-times"></i> 清空权限
                </button>
              </div>
            </div>
            
            <div class="menu-tree">
              <menu-item 
                v-for="menu in filteredMenus" 
                :key="menu.id"
                :menu="menu"
                :selected-role="selectedRole"
                :level="1"
                @toggle-menu="toggleMenu"
                @toggle-menu-with-children="toggleMenuWithChildren"
                @toggle-expand="toggleMenuExpand"
              />
            </div>
          </div>
          
          <div v-else class="empty-state">
            <i class="fas fa-hand-pointer"></i>
            <p>请选择一个角色以查看和编辑权限</p>
            <p class="empty-hint">点击左侧角色列表中的角色项开始配置权限</p>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 角色表单模态框 -->
    <div v-if="showRoleModal" class="modal-overlay" @click="closeRoleModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ formMode === 'add' ? '添加角色' : '编辑角色' }}</h3>
          <button class="close-btn" @click="closeRoleModal">
            <i class="fas fa-times"></i>
          </button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>角色名称 <span class="required">*</span></label>
            <input type="text" v-model="roleForm.name" placeholder="请输入角色名称">
          </div>
          <div class="form-group">
            <label>角色描述</label>
            <textarea v-model="roleForm.description" placeholder="请输入角色描述" rows="3"></textarea>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="closeRoleModal">取消</button>
          <button class="btn btn-primary" @click="saveRole">
            {{ formMode === 'add' ? '添加角色' : '保存更改' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 简化的菜单组件
const MenuItem = {
  name: 'MenuItem',
  props: {
    menu: Object,
    selectedRole: Object,
    level: Number
  },
  methods: {
    isMenuChecked(menuId) {
      if (!this.selectedRole || !this.selectedRole.permissions) return false
      return this.selectedRole.permissions.includes(String(menuId))
    }
  },
  render(h) {
    const { menu, level } = this
    
    const children = []
    if (menu.children && menu.children.length && menu.expanded) {
      menu.children.forEach(child => {
        children.push(
          h('menu-item', {
            props: {
              menu: child,
              selectedRole: this.selectedRole,
              level: level + 1
            },
            on: {
              'toggle-menu': this.$listeners['toggle-menu'],
              'toggle-menu-with-children': this.$listeners['toggle-menu-with-children'],
              'toggle-expand': this.$listeners['toggle-expand']
            }
          })
        )
      })
    }
    
    return h('div', [
      h('div', {
        class: ['menu-item', `level-${level}`],
        style: {
          marginLeft: `${(level - 1) * 20}px`
        }
      }, [
        h('div', {
          class: 'menu-row',
          on: {
            click: () => this.$emit('toggle-expand', menu)
          }
        }, [
          // 展开箭头
          menu.children && menu.children.length > 0 ? h('div', {
            class: 'menu-expand'
          }, [
            h('i', {
              class: ['fas', menu.expanded ? 'fa-chevron-down' : 'fa-chevron-right']
            })
          ]) : h('div', { 
            class: 'menu-expand',
            style: { visibility: 'hidden' }
          }),
          
          // 复选框
          h('div', {
            class: 'menu-checkbox'
          }, [
            h('label', {
              class: 'checkbox-container'
            }, [
              h('input', {
                attrs: { 
                  type: 'checkbox',
                  checked: this.isMenuChecked(menu.id)
                },
                on: {
                  change: (e) => this.$emit('toggle-menu', menu.id, e.target.checked),
                  click: (e) => e.stopPropagation()
                }
              }),
              h('span', {
                class: ['checkmark', this.isMenuChecked(menu.id) ? 'checked' : '']
              })
            ])
          ]),
          
          // 菜单内容
          h('div', {
            class: 'menu-content'
          }, [
            h('div', {
              class: 'menu-icon'
            }, [
              h('i', { 
                class: menu.icon || 'fas fa-file'
              })
            ]),
            h('div', {
              class: 'menu-info'
            }, [
              h('div', { 
                class: 'menu-name'
              }, menu.name),
              menu.path ? h('div', { 
                class: 'menu-path'
              }, menu.path) : null
            ])
          ])
        ])
      ]),
      children.length > 0 ? h('div', { class: 'menu-children' }, children) : null
    ])
  }
}
import { getRoutes, getRoles, addRole, deleteRole, updateRole, getMenuList,saveRoleMenus } from '@/api/role'
export default {
  name: 'RolePermissionSystem',
  components: {
    MenuItem
  },
  data() {
    return {
      roles: [],
      selectedRole: null,
      menuData: [],
      showRoleModal: false,
      formMode: 'add',
      roleForm: {
        id: null,
        name: '',
        description: ''
      },
      roleSearch: '',
      menuSearch: '',
      originalPermissions: [],
      nextRoleId: 5
    }
  },
  computed: {
    filteredRoles() {
      if (!this.roleSearch) return this.roles
      return this.roles.filter(role => 
        role.name.toLowerCase().includes(this.roleSearch.toLowerCase()) ||
        role.description.toLowerCase().includes(this.roleSearch.toLowerCase())
      )
    },
    filteredMenus() {
      if (!this.menuSearch) return this.menuData
      
      const filterMenus = (menus) => {
        return menus.filter(menu => {
          const matches = menu.name.toLowerCase().includes(this.menuSearch.toLowerCase())
          if (menu.children) {
            const filteredChildren = filterMenus(menu.children)
            if (filteredChildren.length > 0) {
              menu.children = filteredChildren
              return true
            }
          }
          return matches
        })
      }
      
      const menusCopy = JSON.parse(JSON.stringify(this.menuData))
      return filterMenus(menusCopy)
    },
    totalMenuCount() {
      const countMenus = (menus) => {
        let count = 0
        menus.forEach(menu => {
          count++
          if (menu.children) {
            count += countMenus(menu.children)
          }
        })
        return count
      }
      return countMenus(this.menuData)
    },
    hasPermissionChanges() {
      if (!this.selectedRole || !this.selectedRole.permissions) return false
      const current = JSON.stringify([...this.selectedRole.permissions].sort())
      const original = JSON.stringify([...this.originalPermissions].sort())
      return current !== original
    }
  },
  created() {
    // 所有角色获取
    this.getRoles()
    // 所有菜单获取
    this.getMenuListData()
  },
  methods: {
    async getRoles() {
      try {
        console.log('开始获取角色列表...')
        const res = await getRoles()
        console.log('获取角色列表完整响应:', res)
        
        if (res && res.code === 200 && res.data) {
          this.roles = res.data.map(role => ({
            ...role,
            permissions: role.permissions || role.menuIds || [] // 兼容不同的字段名
          }))
          console.log('处理后的角色列表:', this.roles)
        } else {
          console.warn('获取角色列表返回数据格式异常:', res)
          // 如果接口返回格式不符合预期，使用空数组
          this.roles = []
        }
      } catch (error) {
        console.error('获取角色列表失败:', error)
        // 出错时使用空数组
        this.roles = []
      }
    },
    async getMenuListData() {
      try {
        // 调用真实的API
        const res = await getMenuList()
        console.log('获取菜单列表响应:', res)
        
        let menuData = []
        if (res && res.data) {
          menuData = res.data
        } else {
          console.warn('获取菜单列表返回数据格式异常:', res)
        }
        
        const processMenus = (menus) => {
          return menus.map(menu => ({
            ...menu,
            expanded: true,
            children: menu.children ? processMenus(menu.children) : []
          }))
        }
        this.menuData = processMenus(menuData)
      } catch (error) {
        console.error('获取菜单列表失败:', error)
        // 出错时使用空数组
        this.menuData = []
      }
    },
    selectRole(role) {
      this.selectedRole = role
      this.originalPermissions = [...(role.permissions || [])]
    },
    getRoleIcon(role) {
      if (role.name === 'admin') return 'fas fa-crown'
      if (role.name.includes('管理员')) return 'fas fa-user-tie'
      return 'fas fa-user'
    },
    getRoleIconClass(role) {
      if (role.name === 'admin') return 'super-admin-icon'
      if (role.name.includes('管理员')) return 'admin-icon'
      return 'user-icon'
    },
    showRoleForm() {
      this.formMode = 'add'
      this.roleForm = {
        id: null,
        name: '',
        description: ''
      }
      this.showRoleModal = true
    },
    editRole(role) {
      this.formMode = 'edit'
      this.roleForm = {
        id: role.id,
        name: role.name,
        description: role.description
      }
      this.showRoleModal = true
    },
    async deleteRole(role) {
      if (role.name === 'admin') {
        alert('超级管理员角色不能被删除')
        return
      }
      
      if (confirm(`确定要删除角色 "${role.name}" 吗？此操作不可恢复。`)) {
        try {
          await deleteRole(role.id)
          // 删除成功后更新本地数据
          const index = this.roles.findIndex(r => r.id === role.id)
          if (index !== -1) {
            this.roles.splice(index, 1)
            if (this.selectedRole && this.selectedRole.id === role.id) {
              this.selectedRole = null
            }
          }
        } catch (error) {
          console.error('删除角色失败:', error)
          alert('删除角色失败，请重试')
        }
      }
    },
    async saveRole() {
      if (!this.roleForm.name.trim()) {
        alert('角色名称不能为空')
        return
      }
      
      try {
        if (this.formMode === 'add') {
          // 准备新增角色的数据
          const newRoleData = {
            name: this.roleForm.name,
            description: this.roleForm.description || '',
          }
          
          console.log('新增角色数据:', newRoleData)
          
          // 调用API添加角色
          const res = await addRole(newRoleData)
          console.log('新增角色响应:', res)
          
          if (res && res.code === 200) {
            // 重新获取角色列表以确保数据一致
            await this.getRoles()
            alert('角色添加成功')
          } else {
            alert(res?.msg || '添加角色失败')
          }
        } else {
          // 编辑角色的逻辑
          const updateData = {
            id: this.roleForm.id,
            name: this.roleForm.name,
            description: this.roleForm.description || ''
          }
          
          console.log('更新角色数据:', updateData)
          
          const res = await updateRole(updateData)
          console.log('更新角色响应:', res)
          
          if (res && res.code === 200) {
            // 更新本地数据
            const index = this.roles.findIndex(r => r.id === this.roleForm.id)
            if (index !== -1) {
              this.roles[index].name = this.roleForm.name
              this.roles[index].description = this.roleForm.description
            }
            alert('角色更新成功')
          } else {
            alert(res?.msg || '更新角色失败')
          }
        }
        
        this.closeRoleModal()
      } catch (error) {
        console.error('保存角色失败:', error)
        alert('保存角色失败: ' + (error.message || '请重试'))
      }
    },
    // 自动获取权限：根据角色名称自动匹配菜单ID
    getAutoPermissions() {
      const roleName = this.roleForm.name.toLowerCase()
      const allMenuIds = this.getAllMenuIds()
      
      if (roleName.includes('admin') || roleName.includes('管理员')) {
        return allMenuIds
      } else if (roleName.includes('user')) {
        return allMenuIds.filter(id => ['1000', '1001', '3000'].includes(String(id)))
      } else {
        return ['1000', '3000']
      }
    },
    getAllMenuIds() {
      const ids = []
      const collectIds = (menus) => {
        menus.forEach(menu => {
          ids.push(String(menu.id))
          if (menu.children) {
            collectIds(menu.children)
          }
        })
      }
      collectIds(this.menuData)
      return ids
    },
    closeRoleModal() {
      this.showRoleModal = false
    },
    toggleMenu(menuId, checked) {
      if (!this.selectedRole) return
      
      const menuIdStr = String(menuId)
      
      if (checked) {
        if (!this.selectedRole.permissions.includes(menuIdStr)) {
          this.selectedRole.permissions.push(menuIdStr)
        }
      } else {
        const index = this.selectedRole.permissions.indexOf(menuIdStr)
        if (index !== -1) {
          this.selectedRole.permissions.splice(index, 1)
        }
      }
    },
    toggleMenuWithChildren(menu, checked) {
      this.toggleMenu(menu.id, checked)
      
      const toggleChildren = (menuItem, checked) => {
        this.toggleMenu(menuItem.id, checked)
        if (menuItem.children) {
          menuItem.children.forEach(child => toggleChildren(child, checked))
        }
      }
      
      if (menu.children) {
        
        menu.children.forEach(child => toggleChildren(child, checked))
      }
    },
    expandAllMenus() {
      const expand = (menus) => {
        menus.forEach(menu => {
          menu.expanded = true
          if (menu.children) {
            expand(menu.children)
          }
        })
      }
      expand(this.menuData)
    },
    collapseAllMenus() {
      const collapse = (menus) => {
        menus.forEach(menu => {
          menu.expanded = false
          if (menu.children) {
            collapse(menu.children)
          }
        })
      }
      collapse(this.menuData)
    },
    toggleMenuExpand(menu) {
      menu.expanded = !menu.expanded
    },
    selectAllMenus() {
      if (!this.selectedRole) return
      this.selectedRole.permissions = this.getAllMenuIds()
    },
    clearAllPermissions() {
      if (!this.selectedRole) return
      if (confirm('确定要清空该角色的所有权限吗？')) {
        this.selectedRole.permissions = []
      }
    },
    getSelectedMenuCount() {
      return this.selectedRole ? (this.selectedRole.permissions || []).length : 0
    },
    async savePermissions() {
      if (!this.selectedRole) return
      
      try {
        // 调用API保存权限
        await saveRoleMenus(this.selectedRole.id, this.selectedRole.permissions)
        
        alert(`角色 "${this.selectedRole.name}" 的权限已保存`)
        console.log('保存的权限:', this.selectedRole.permissions)
        
        this.originalPermissions = [...(this.selectedRole.permissions || [])]
      } catch (error) {
        console.error('保存权限失败:', error)
        alert('保存权限失败，请重试')
      }
    }
  }
}
</script>

<style scoped>
.role-permission-system {
  width: 100%;
  height: 100vh;
  background: #f5f7fa;
  display: flex;
  flex-direction: column;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0;
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
}

.header-content {
  padding: 20px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.header-icon {
  font-size: 32px;
  opacity: 0.9;
}

.header h1 {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.header-subtitle {
  font-size: 14px;
  opacity: 0.8;
  margin: 0;
  margin-top: 4px;
}

.header-stats {
  display: flex;
  gap: 30px;
}

.stat-item {
  text-align: center;
}

.stat-number {
  display: block;
  font-size: 24px;
  font-weight: 700;
}

.stat-label {
  font-size: 12px;
  opacity: 0.8;
}

.content {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 20px;
  padding: 20px;
}

.role-panel {
  width: 350px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.menu-panel {
  flex: 1;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  padding: 20px 24px;
  background: white;
  border-bottom: 1px solid #eef2f7;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 15px;
}

.panel-header h2 {
  color: #2c3e50;
  font-size: 18px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
}

.selected-role {
  color: #667eea;
  font-weight: 500;
}

.permission-stats {
  margin-left: 10px;
}

.stat-badge {
  background: #edf2f7;
  color: #4a5568;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.stat-badge i {
  color: #48bb78;
  margin-right: 4px;
}

.panel-actions {
  display: flex;
  gap: 8px;
}

.panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 0;
}

.role-search, .menu-search {
  padding: 16px 20px;
  border-bottom: 1px solid #eef2f7;
  position: relative;
}

.search-input {
  width: 100%;
  padding: 10px 15px 10px 40px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
}

.search-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.search-icon {
  position: absolute;
  left: 35px;
  top: 50%;
  transform: translateY(-50%);
  color: #a0aec0;
}

.role-list {
  padding: 8px;
}

.role-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-radius: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;
  background-color: white;
  position: relative;
  padding-right: 80px; /* 为操作按钮留出空间 */
}

.role-item:hover {
  background-color: #f7fafc;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.role-item.active {
  background-color: #f0f4ff;
  border-color: #667eea;
}

.role-item.super-admin {
  background: linear-gradient(135deg, #fff9e6 0%, #fff0cc 100%);
  border-color: #ffd700;
}

.role-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 18px;
  transition: all 0.3s;
}

.super-admin-icon {
  background: linear-gradient(135deg, #ffd700 0%, #ffed4a 100%);
  color: #8b6914;
}

.admin-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.user-icon {
  background: linear-gradient(135deg, #a0aec0 0%, #718096 100%);
  color: white;
}

.role-info {
  flex: 1;
  min-width: 0;
}

.role-name {
  font-weight: 600;
  color: #2d3748;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.super-admin-badge {
  color: #ffd700;
  font-size: 14px;
}

.role-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  font-weight: 600;
}

.super-admin-tag {
  background: #ffd700;
  color: #8b6914;
}

.role-tag:not(.super-admin-tag) {
  background: #e2e8f0;
  color: #4a5568;
}

.role-desc {
  font-size: 13px;
  color: #718096;
  margin-bottom: 6px;
}

.role-meta {
  display: flex;
  align-items: center;
  gap: 15px;
}

.permission-count {
  font-size: 12px;
  color: #a0aec0;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 关键修复：操作按钮样式 */
.role-actions {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  display: flex !important;
  gap: 8px;
  opacity: 1 !important;
  visibility: visible !important;
  z-index: 10;
}

.btn-icon {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  background: #f8fafc;
  color: #64748b;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  font-size: 14px;
}

.btn-icon:hover {
  background: #e2e8f0;
  color: #374151;
}

.btn-icon:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-icon:disabled:hover {
  background: #f8fafc;
  color: #64748b;
}

.menu-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #f1f5f9;
  background: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.menu-actions {
  display: flex;
  gap: 8px;
}

.menu-tree {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: #fafbfc;
}

.menu-item {
  position: relative;
  transition: all 0.2s ease;
  margin-bottom: 2px;
}

.menu-row {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  border-radius: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
  gap: 12px;
  background: white;
  border: 1px solid #f1f5f9;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
}

.menu-row::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  opacity: 0;
  transition: opacity 0.3s;
}

.menu-row:hover {
  border-color: #e2e8f0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transform: translateY(-1px);
}

.menu-row:hover::before {
  opacity: 1;
}

.menu-expand {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #64748b;
  font-size: 12px;
  transition: all 0.3s;
  flex-shrink: 0;
  border-radius: 4px;
  background: #f8fafc;
}

.menu-expand:hover {
  background: #e2e8f0;
  color: #475569;
}

.menu-checkbox {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.menu-content {
  display: flex;
  align-items: center;
  flex: 1;
  gap: 12px;
  min-width: 0;
}

.menu-icon {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  transition: all 0.3s;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.menu-row:hover .menu-icon {
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.menu-info {
  flex: 1;
  min-width: 0;
}

.menu-name {
  font-weight: 500;
  color: #1e293b;
  margin-bottom: 4px;
  transition: color 0.2s;
  font-size: 14px;
  letter-spacing: 0.2px;
}

.menu-path {
  font-size: 12px;
  color: #64748b;
  font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', 'Source Code Pro', monospace;
  word-break: break-all;
  opacity: 0.8;
}

.menu-badge .badge {
  padding: 4px 10px;
  border-radius: 16px;
  font-size: 11px;
  font-weight: 600;
  flex-shrink: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
}

.menu-children {
  margin-top: 4px;
  animation: slideDown 0.3s ease;
  position: relative;
}

/* 复选框样式 */
.checkbox-container {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.checkbox-container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}

.checkmark {
  height: 20px;
  width: 20px;
  background-color: #fff;
  border: 2px solid #cbd5e1;
  border-radius: 6px;
  position: relative;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.checkmark:hover {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.checkmark.checked {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: transparent;
  box-shadow: 0 2px 6px rgba(102, 126, 234, 0.4);
}

.checkmark:after {
  content: "";
  position: absolute;
  display: none;
}

.checkmark.checked:after {
  display: block;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg) translate(-1px, -1px);
}

/* 动画效果 */
@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 层级指示器 */
.menu-item.level-2 .menu-row {
  background: #fefefe;
  border-left: 2px solid #e2e8f0;
}

.menu-item.level-3 .menu-row {
  background: #fdfdfd;
  border-left: 2px solid #e2e8f0;
  margin-left: 8px;
}

/* 滚动条美化 */
.menu-tree::-webkit-scrollbar {
  width: 8px;
}

.menu-tree::-webkit-scrollbar-track {
  background: #f1f5f9;
  border-radius: 4px;
}

.menu-tree::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
}

.menu-tree::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* 空状态样式 */
.empty-state {
  text-align: center;
  color: #94a3b8;
  padding: 60px 20px;
  font-style: italic;
  background: white;
  border-radius: 12px;
  margin: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.empty-state i {
  font-size: 40px;
  margin-bottom: 10px;
  opacity: 0.7;
}

.empty-hint {
  font-size: 13px;
  margin-top: 8px;
  opacity: 0.7;
}

/* 菜单搜索区域优化 */
.menu-search {
  position: relative;
  flex: 1;
  max-width: 300px;
}

.menu-search .search-input {
  padding: 12px 16px 12px 44px;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #f8fafc;
  transition: all 0.3s;
  font-size: 14px;
}

.menu-search .search-input:focus {
  background: white;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.menu-search .search-icon {
  left: 16px;
  color: #94a3b8;
}

/* 按钮样式 */
.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 2px 6px rgba(102, 126, 234, 0.3);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: #e2e8f0;
  color: #4a5568;
}

.btn-secondary:hover:not(:disabled) {
  background: #cbd5e0;
}

.btn-outline {
  background: transparent;
  color: #667eea;
  border: 1px solid #667eea;
}

.btn-outline:hover:not(:disabled) {
  background: #667eea;
  color: white;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid #eef2f7;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fafbfc;
}

.modal-header h3 {
  margin: 0;
  color: #2d3748;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 18px;
  color: #a0aec0;
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  transition: all 0.2s;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  background-color: #edf2f7;
  color: #4a5568;
}

.modal-body {
  padding: 24px;
}

.modal-footer {
  padding: 20px 24px;
  border-top: 1px solid #eef2f7;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  background: #fafbfc;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #4a5568;
}

.required {
  color: #e53e3e;
}

.form-group input, 
.form-group textarea,
.form-select {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
  font-family: inherit;
}

.form-group input:focus, 
.form-group textarea:focus,
.form-select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-select {
  background-color: white;
  cursor: pointer;
}

/* 滚动条样式 */
.panel-content::-webkit-scrollbar {
  width: 6px;
}

.panel-content::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.panel-content::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 3px;
}

.panel-content::-webkit-scrollbar-thumb:hover {
  background: #a0aec0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content {
    flex-direction: column;
  }
  
  .role-panel {
    width: 100%;
  }
  
  .menu-header {
    flex-direction: column;
    gap: 12px;
    align-items: stretch;
  }
  
  .menu-search {
    max-width: none;
  }
  
  .menu-actions {
    justify-content: flex-end;
  }
  
  .panel-actions {
    flex-wrap: wrap;
  }
}

/* 强制显示操作按钮的样式 */
.role-actions {
  display: flex !important;
  visibility: visible !important;
  opacity: 1 !important;
}
</style>